package com.power.using.myapplication.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.power.using.myapplication.R;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class FirstActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



    }

    public void onclick(View view) {

//        String url = "http://192.168.9.113:8080/WebTest03/TestServlet";
//        OkHttpUtils
//                .postString()
//                .url(url)
//                .mediaType(MediaType.parse("application/json; charset=utf-8"))
//                //.mediaType("")
//                .content(new Gson().toJson(new User("aaa", "123")))
//                .build()
//                .execute(new MyCallBack() {
//
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//                        tv.setText(e.getMessage());
//                        Log.i("callbackData",e.getMessage());
//
//                    }
//
//                    @Override
//                    public void onResponse(UserBack response, int id) {
//                        tv.setText(response.toString());
//
//                        Log.i("callbackData", response.toString());
//
//                    }
//                });
//                .execute(new StringCallback() {
//
//
//                    @Override
//                    public void onResponse(String response, int id) {
//                        tv.setText(response);
//
//                        Log.i("callbackData",response);
//                    }
//
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//                        tv.setText(e.getMessage());
//                        Log.i("callbackData",e.getMessage());
//                    }
//                });

        /*String url = "http://192.168.9.119:12345/Begin/route/pageQuery";
        OkHttpUtils
                .post()
                .url(url)
                .addParams("cid","5")
                .addParams("currentPage","1")
                .addParams("rname","")
                .build()
                .execute(new StringCallback() {

                    @Override
                    public void onResponse(String response, int id) {

                        Log.i("aa",response);
                    }


                    @Override
                    public void onError(Call call, Exception e, int id) {

                        Log.i("aa",e.getMessage());
                    }
                });*/


        String url = "http://192.168.9.119:12345/Begin/route/pageQuery";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("aatttt", "onFailure: "+e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("aatttt", "onResponse: " + response.body().string());
            }
        });


    }

    public void ShowMess(View view){

       /* MediaType mediaType = MediaType.parse("text/x-markdown; charset=utf-8");
        String requestBody = "I am Jdqm.";
        Request request = new Request.Builder()
                .url("http://192.168.9.119:12345/Begin/route/pageQuery")
                .post(RequestBody.create(mediaType, requestBody))
                .build();
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("onFailure", "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("onResponse", response.protocol() + " ~~ " +response.code() + " " + response.message());
                Headers headers = response.headers();
                for (int i = 0; i < headers.size(); i++) {
                    Log.d("aaonResponse", headers.name(i) + ":" + headers.value(i));
                }
                Log.d("onResponse", "onResponse: " + response.body().string());
            }
        });*/

        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("cid","5")
                .add("currentPage","1")
                .add("rname","")
                .build();
        Request request = new Request.Builder()
                .url("http://192.168.9.119:12345/Begin/route/pageQuery")
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("onFailure", "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("onResponse", response.protocol() + " ~~ " +response.code() + " " + response.message());
                Headers headers = response.headers();
                for (int i = 0; i < headers.size(); i++) {
                    Log.d("aaonResponse", headers.name(i) + ":" + headers.value(i));
                }
                Log.d("onResponse", "onResponse: " + response.body().string());
            }
        });

    }
}
